//
//  Task_ViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/5.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "Task_ViewController.h"
#import "BaseTableView.h"
#import "BaseTableViewCell.h"
#import "DLMacro.h"
#import "Task_tableViewCell.h"
#import "Hot_ViewController.h"
#import "PersonalInfo_ViewController.h"
#import "Offer_ViewController.h"
#import "DLWNetworkingParser.h"
#import "ProductEffect_ViewController.h"
@interface Task_ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSArray *arrData;
@end

@implementation Task_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"每日任务";
    // Do any additional setup after loading the view.
    [self handleData];
    [self createTableView];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 获取是否签到了
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    NSString *ticket = [userDefault valueForKey:@"ticket"];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:ticket,@"ticket", nil];

    [DLWNetworkingParser POSTWithURL:@"http://api.danjiguanjia.com/v1/Score/sign_pd" par:dic success:^(id responseObject) {
        
        NSString *msg = responseObject[@"msg"];
        if ([msg isEqualToString:@"今天已经签到"]) {
            _signIn = @"签到成功";
            [_tableView reloadData];
        }
    } filed:^(NSError *error) {
        
    }];

}


- (void)handleData
{
    _arrData = [NSArray arrayWithObjects:@"每日签到", @"完善个人信息", @"每日文章分享", @"鸡蛋报价", @"原料报价", @"生产效益", nil];
}

- (void)createTableView
{
    _tableView = [[BaseTableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self createTableViewHeaderView];
    [self.view addSubview:_tableView];
    // 注册cell;
    [_tableView registerClass:[Task_tableViewCell class] forCellReuseIdentifier:@"reuse"];
}
#pragma mark -tableView的协议方法
// cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Task_tableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    NSString *string = _arrData[indexPath.row];
    cell.lable.text = string;
    
    if ([_signIn isEqualToString:@"签到成功"] && indexPath.row == 0) {
        cell.lable.textColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
        cell.iamgeview.image = [UIImage imageNamed:@"duigou_s"];
        
    }
    return cell;
}


// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

// 创建tableView的头视图
- (void)createTableViewHeaderView
{
    BaseLabel *lable = [[BaseLabel alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 60)];
    lable.text = @"每日任务";
    lable.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    _tableView.tableHeaderView = lable;
}

#pragma mark -点击每个cell

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if (indexPath.row == 1)
    {
        PersonalInfo_ViewController *perInfoVC = [[PersonalInfo_ViewController alloc] init];
        [self.navigationController pushViewController:perInfoVC animated:YES];
    }
    else if (indexPath.row == 2)
    {
        Hot_ViewController *hotVC = [[Hot_ViewController alloc] init];
        [self.navigationController pushViewController:hotVC animated:YES];
    } else if (indexPath.row == 3 || indexPath.row == 4)
    {
        Offer_ViewController *OfferVC = [[Offer_ViewController alloc] init];
        [self.navigationController pushViewController:OfferVC animated:YES];
    }else
    {
        ProductEffect_ViewController *productVC = [[ProductEffect_ViewController alloc] init];
        [self.navigationController pushViewController:productVC animated:YES];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
